//
//  SEGPhoneRechargeViewController.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/2/28.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGPhoneRechargeViewController.h"
#import "SEGPhoneRechargeView.h"
#import "SEGPhoneRechargeConfirmView.h"
#import "SEGQueryFeedbackViewController.h"
#import "SEGPhoneRechargeViewModel.h"

@interface SEGPhoneRechargeViewController ()<SEGPhoneRechargeViewDelegate,SEGPhoneRechargeConfirmViewDelegate>
@property (nonatomic, strong)SEGPhoneRechargeView *mainView;
@property (nonatomic, strong)SEGPhoneRechargeConfirmView *confirmView;

@property(nonatomic, strong) SEGPhoneRechargeViewModel *viewModel;
@end

@implementation SEGPhoneRechargeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"手机话费充值";
    
    self.mainView = [[SEGPhoneRechargeView alloc] init];
    [self.view addSubview:self.mainView];
    [self.mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    self.mainView.viewDelegate = self;
    
    //已存在提交时间 直接显示联系客服
    NSString *applyTime = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"isSubmitPhone%@%@",kAccountUserID,self.turnTip]];
    if (applyTime && applyTime != nil & applyTime.length > 0) {
        [self.mainView showFinishViewWithApplyTime:applyTime];
    }
    
    [self loadBottomAd];
}

//加载底部广告
- (void)loadBottomAd
{
    [self.viewModel segRequestAdvlistWithPositionType:^(BOOL isSuccessed, NSURL * _Nonnull pic) {
        if (isSuccessed) {
            [self.mainView.adImageView setImageWithURL:pic placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"pic_default_300_300")] options:SDWebImageRetryFailed];
        }
    }];
}

/**
 *  显示确认界面
 */
- (void)showConfirmViewWithPhone:(NSString *)phoneNumber
{
    if (!self.confirmView) {
        self.confirmView = [[SEGPhoneRechargeConfirmView alloc] initWithFrame:CGRectMake(0, 0, LYScreenWidth(), LYScreenHeight())];
        self.confirmView.viewDelegate = self;
        [[[UIApplication sharedApplication] keyWindow] addSubview:self.confirmView];
    }
    self.confirmView.hidden = NO;
    self.confirmView.phoneLabel.text = phoneNumber;
}

/**
 *  显示联系客服
 */
- (void)contactCustomerService
{
    SEGQueryFeedbackViewController *feedbackViewController = [[SEGQueryFeedbackViewController alloc] init];
    [self.navigationController pushViewController:feedbackViewController animated:YES];
}

/**
 *  确认手机号
 */
- (void)confirmPhoneNumber
{
    [SVProgressHUD show];
    //提交手机号
    [self.viewModel requestSubmitPhoneByChargeWithWinId:self.turnTip phone:self.confirmView.phoneLabel.text completed:^(NSString * _Nonnull codeType, NSString * _Nonnull errorMessage) {
        [SVProgressHUD dismiss];
        if ([codeType isEqualToString:@"0"]) {
            [self.mainView showFinishViewWithApplyTime:self.viewModel.inviteManager.applyTime];
        }else {
            [SEGAlertView showTmepMessageWithMessage:errorMessage];
        }
    }];
}


#pragma mark - init Views
-(void)segInitViews{

}

#pragma mark - RAC Data Binding
- (void)bindViewModel {
}

#pragma mark - getter
-(SEGPhoneRechargeViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[SEGPhoneRechargeViewModel alloc]init];
    }
    return _viewModel;
}
@end
